package postwp.controller;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import data.Data;
import java.io.IOException;
import java.io.PrintStream;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import util.Utils;

public class HomeServlet extends HttpServlet {
	private static ActiveMQConnectionFactory connectionFactory;
	private static Connection connection = null;
	private static Session session;
	private static Destination destination;
	private static boolean transacted = false;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		HttpSession session = req.getSession();
		if (session.getAttribute("users") != null)
			try {
				req.getRequestDispatcher("/index.jsp").forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		HttpSession session = req.getSession();
		if (session.getAttribute("users") != null) {
			DBObject usersLogin = (DBObject) session.getAttribute("users");
			String url = Utils.fomatDomain(req.getParameter("url"));
			url = "http://" + url;
			String reg_title = req.getParameter("reg_title");
			String reg_content = req.getParameter("reg_content");
			String reg_link = "comments";
			String domain = (String) usersLogin.get("url");
			String reg_remove = req.getParameter("reg_remove");

			DBCollection collection = Data.data("domaincraw");
			DBObject check = collection.findOne(new BasicDBObject("url", url));
			if (check == null) {
				BasicDBObject obj = new BasicDBObject();
				BasicDBObject info = new BasicDBObject();
				info.put(domain.replaceAll("[.]{1}", "&&&"), Integer.valueOf(1));
				obj.put("url", url);
				obj.put("reg_title", reg_title);
				obj.put("reg_content", reg_content);
				obj.put("domain", info);
				obj.put("reg_link", reg_link);
				obj.put("reg_remove", reg_remove);
				collection.insert(new DBObject[] { obj });
				System.out.println(obj);
				try {
					setUp();
					createProducerAndSendAMessage(url);
				} catch (JMSException e) {
					e.printStackTrace();
				}
			} else {
				DBObject dbDomain = (DBObject) check.get("domain");
				if (dbDomain.get(domain.replaceAll("[.]{1}", "&&&")) == null) {
					dbDomain.put(domain.replaceAll("[.]{1}", "&&&"),
							Integer.valueOf(1));
					check.put("domain", dbDomain);
					collection.save(check);
				}
			}
			try {
				req.getRequestDispatcher("/index.jsp").forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
	}

	private static void setUp() throws JMSException, IOException {
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_USER,
				ActiveMQConnection.DEFAULT_PASSWORD, "tcp://y1s.org:61616");
		if (connection == null) {
			connection = connectionFactory.createConnection();
			connection.start();
		}
		session = connection.createSession(transacted, 1);

		destination = session.createQueue("getlinkcraw");
	}

	private static void createProducerAndSendAMessage(String str)
			throws JMSException {
		MessageProducer producer = session.createProducer(destination);
		producer.setDeliveryMode(1);
		TextMessage message = session.createTextMessage(str);
		System.out.println("Sending message: " + message.getText());
		producer.send(message);
	}
}